package com.eric.media.recorder;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.SurfaceView;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * 播放网络音频播放工具类
 * 
 */
public class PlayerUtil implements OnBufferingUpdateListener, OnCompletionListener,
		MediaPlayer.OnPreparedListener {

	public MediaPlayer mediaPlayer = null;
	private MyPlayerCallback callback;
	/** 线程池*/
	private ExecutorService executorService = Executors.newFixedThreadPool(10);

	/**
	 * 有参构造方法
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,10:11
	 * <h3>UpdateTime</h3> 2016/4/21,10:11
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 */
	public PlayerUtil() {
		createMediaPlayer();
	}

	/**
	 * 创建MediaPlayer
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/19,22:34
	 * <h3>UpdateTime</h3> 2016/4/19,22:34
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 */
	private void createMediaPlayer() {
		try {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			//buffer的进度进行监听
			mediaPlayer.setOnBufferingUpdateListener(this);
			//播放准备监听
			mediaPlayer.setOnPreparedListener(this);
			//播放完成监听
			mediaPlayer.setOnCompletionListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 新开一个线程，播放音频。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:58
	 * <h3>UpdateTime</h3> 2016/4/21,9:58
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param videoUrl 音频路径
	 */
	public void playUrl(final String videoUrl) {
		if (mediaPlayer == null) {
			createMediaPlayer();
		}
		//把播放音频的线程提交到线程池中
		executorService.submit(new Runnable() {
			@Override
			public void run() {
				try {
					mediaPlayer.reset();
					mediaPlayer.setDataSource(videoUrl);
					mediaPlayer.prepare();
					mediaPlayer.start();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * 新开一个线程，播放音频。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:47
	 * <h3>UpdateTime</h3> 2016/4/21,9:47
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param videoUrl 音频地址
	 */
	public void playVideoUrl(final String videoUrl, final SurfaceView playView) {
		if (mediaPlayer == null) {
			createMediaPlayer();
		}
		executorService.submit(new Runnable() {
			@Override
			public void run() {
				try {
					mediaPlayer.reset();
					mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
					//设置需要播放的视频
//            String path = movieRV.getmVecordFile().getAbsolutePath();
					mediaPlayer.setDataSource(videoUrl);

//					Log.d("play:", videoUrl);
					//把视频画面输出到SurfaceView
					mediaPlayer.setDisplay(playView.getHolder());
					mediaPlayer.prepare();
					//播放
					mediaPlayer.start();
				}  catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 *  开始播放（暂停后开启播放）
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:40
	 * <h3>UpdateTime</h3> 2016/4/21,9:40
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public void play() {
		mediaPlayer.start();
	}



	/**
	 * 暂停播放播放
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:40
	 * <h3>UpdateTime</h3> 2016/4/21,9:40
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public void pause() {
		mediaPlayer.pause();
	}

	/**
	 * 停止播放 释放资源
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:40
	 * <h3>UpdateTime</h3> 2016/4/21,9:40
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public void stop() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	/**
	 * 设置播放回调接口
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:41
	 * <h3>UpdateTime</h3> 2016/4/21,9:41
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param callback
	 */
	public void setMyPlayerCallback(MyPlayerCallback callback) {
		this.callback = callback;
	}

	/**
	 * 通过onPrepared播放
	 */
	@Override
	public void onPrepared(MediaPlayer arg0) {
		arg0.start();
		if (callback != null) {
			callback.onPrepared();
		}
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		if (callback != null) {
			callback.onCompletion();
		}
		mediaPlayer.stop();
	}

	/**
	 * 播放进度
	 * @param arg0
	 * @param bufferingProgress
	 */
	@Override
	public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {

	}


	/**
	 * 播放回调接口
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/4/21,9:38
	 * <h3>UpdateTime</h3> 2016/4/21,9:38
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public interface MyPlayerCallback {
		/**
		 * 播放前准备
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/4/21,9:38
		 * <h3>UpdateTime</h3> 2016/4/21,9:38
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 */
		 void onPrepared();
		/**
		 * 播放结束
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/4/21,9:38
		 * <h3>UpdateTime</h3> 2016/4/21,9:38
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 */
		 void onCompletion();

	}




}